﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccess_Layer
{
    public static class ChungLoaiSPDAC
    {
        public static object layHet()
        {
            return DataAccessHelper.DB.ChungLoaiSPs.Select(cl => cl);
        }
        public static object tim(long? tkID, string tkTen)
        {
            if (tkTen != null && tkID != null)
            {
                return DataAccessHelper.DB.ChungLoaiSPs.Where(cl => cl.ID == tkID && cl.Ten.Contains(tkTen)).Select(cl => cl);
            }
            else if (tkTen != null && tkID == null)
            {
                return DataAccessHelper.DB.ChungLoaiSPs.Where(cl => cl.Ten.Contains(tkTen)).Select(cl => cl);
            }
            else if (tkTen == null && tkID != null)            
            {
                return DataAccessHelper.DB.ChungLoaiSPs.Where(cl => cl.ID == tkID).Select(cl => cl);
            }

            return layHet();
        }

        public static void delete(List<long> deletedIDs)
        {
            foreach (long deletedID in deletedIDs)
            {
                ChungLoaiSP clsp = DataAccessHelper.DB.ChungLoaiSPs.Where(ncc => ncc.ID == deletedID).Select(ncc => ncc).First();
                DataAccessHelper.DB.ChungLoaiSPs.DeleteOnSubmit(clsp);
            }

            DataAccessHelper.Save();
        }

        public static void update(ChungLoaiSP chungLoaiSP)
        {
            ChungLoaiSP clsp = DataAccessHelper.DB.ChungLoaiSPs.Where(ncc => ncc.ID == chungLoaiSP.ID).Select(ncc => ncc).First();
            clsp.Hinh = chungLoaiSP.Hinh;
            clsp.Ten = chungLoaiSP.Ten;
            clsp.DaXoa = chungLoaiSP.DaXoa;            

            DataAccessHelper.Save();
        }
    }
}
